package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.service.adminservice.KeywordService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 楚夏三分
 * @datetime 2022/12/29 17:29
 * @description: TODO:
 */
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeywordMapper keywordMapper;

    @Override
    public CommonData<MarketKeyword> list(BasePageInfo info, String url, String keyword) {
// 开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());
        // 条件查询
        MarketKeywordExample example = new MarketKeywordExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        // 模糊查询
        if (!StringUtil.isEmpty(url)){
            criteria.andUrlLike("%" + url + "%");
        }
        if (!StringUtil.isEmpty(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        // 查询逻辑存在
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywordList = keywordMapper.selectByExample(example);
        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(marketKeywordList);
        return CommonData.data(marketKeywordPageInfo);
    }

    @Override
    public MarketKeyword create(Boolean isDefault, Boolean isHot, String keyword, String url) {
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setIsDefault(isDefault);
        marketKeyword.setIsHot(isHot);
        marketKeyword.setKeyword(keyword);
        marketKeyword.setUrl(url);
        marketKeyword.setAddTime(new Date());
        marketKeyword.setDeleted(false);
        keywordMapper.insertSelective(marketKeyword);
        return marketKeyword;
    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKey(marketKeyword);
        return marketKeyword;
    }

    @Override
    public void delete(Integer id) {
        keywordMapper.deleteByIdLogically(id);
    }
}
